﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BusinessEntities;

namespace WindowsFormsApp
{
    public partial class AlterDataDlg : Form
    {
        public ILocatingPoint ReturnValue { get; private set; }

        public ILocatingPoint type;


        public AlterDataDlg(string title)
        {
            InitializeComponent();

            this.Text = title;
        }

        public AlterDataDlg(string tilte, ILocatingPoint t) : this(tilte)
        {
            type = t;

            if (t is MetroNode)
            {
                SetMetroMode();

                if (t.Name==null || t.Name == "") return; //添加数据


                MetroNode data = t as MetroNode;

                txt_metro_name.Text = data.Name;
                txt_metro_x.Text = data.X.ToString();
                txt_metro_y.Text = data.Y.ToString();

                if (data.IsTerminal)
                {
                    rdo_metro_yes.Checked = true;
                }
                else
                {
                    rdo_metro_no.Checked = true;
                }

                txt_metro_line1.Text = data.Lines[0].ToString();
                if (data.Lines.Count > 1)
                    txt_metro_line2.Text = data.Lines[1].ToString();

            }
            else
            {
                SetOtherMode();

                if (t.Name == "") return; //添加数据

                IPeriphery data = t as IPeriphery;

                txt_other_name.Text = data.Name;
                txt_other_x.Text = data.X.ToString();
                txt_other_y.Text = data.Y.ToString();
                txt_other_grade.Text = data.Grade.ToString();

            }

        }

        private void SetMetroMode()
        {
            pnl_other.Visible = false;
            pnl_metro.Visible = true;
            this.Size = new Size(210, 215);
            btn_confirm.Location = new Point(12, 140);
            btn_cancel.Location = new Point(109, 140);
        }

        private void SetOtherMode()
        {
            pnl_metro.Visible = false;
            pnl_other.Visible = true;
            this.Size = new Size(210, 190);
            btn_confirm.Location = new Point(12, 120);
            btn_cancel.Location = new Point(109, 120);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (type is MetroNode)
            {
                if (txt_metro_name.Text == "") { MessageBox.Show("名称不能为空"); return; }
                if (txt_metro_x.Text == "") { MessageBox.Show("坐标x不能为空"); return; }
                if (txt_metro_y.Text == "") { MessageBox.Show("坐标y不能为空"); return; }
                if (txt_metro_line1.Text == "") { MessageBox.Show("至少要有一条线路"); txt_metro_line2.Text = ""; return; }

                int x, y, line1;
                int line2 = -1;
                try
                {
                    x = int.Parse(txt_metro_x.Text);
                    y = int.Parse(txt_metro_y.Text);
                    line1 = int.Parse(txt_metro_line1.Text);
                    if (txt_metro_line2.Text != "")
                        line2 = int.Parse(txt_metro_y.Text);
                }
                catch (Exception exception)
                {
                    MessageBox.Show("坐标x，y只能输入整数");
                    return;
                }
                ReturnValue = new MetroNode(-1, txt_metro_name.Text, x, y, rdo_metro_yes.Checked, line1, line2);

            }
            else
            {
                if (txt_other_name.Text == "") { MessageBox.Show("名称不能为空"); return; }
                if (txt_other_x.Text == "") { MessageBox.Show("坐标x不能为空"); return; }
                if (txt_other_y.Text == "") { MessageBox.Show("坐标y不能为空"); return; }
                if (txt_other_grade.Text == "") { MessageBox.Show("评分不能为空"); return; }

                int x, y, grade;
                try
                {
                    x = int.Parse(txt_other_x.Text);
                    y = int.Parse(txt_other_y.Text);
                    grade = int.Parse(txt_other_grade.Text);
                }
                catch (Exception exception)
                {
                    MessageBox.Show("坐标x，y,grade只能输入整数");
                    return;
                }

                if (type is Grogshop)
                {
                    ReturnValue = new Grogshop(-1, txt_other_name.Text, x, y, grade);
                }
                else if (type is Restaurant)
                {
                    ReturnValue = new Restaurant(-1, txt_other_name.Text, x, y, grade);
                }
                else if (type is Supermarket)
                {
                    ReturnValue = new Supermarket(-1, txt_other_name.Text, x, y, grade);
                }
            }

            DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}
